package com.sand.service.Imp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sand.dao.IBaseDao;
import com.sand.entity.*;
import com.sand.service.IQuestionService;
import com.sand.util.PageModel;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "questionService")
public class QuestionServiceImp implements IQuestionService {
/**
 *
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/12/2/002 16:36
 */
    //Dao层接口
    @Resource(name = "baseDao")
    private IBaseDao baseDao;

    //跟进id查询提问
    @Override
    public Map<String,Object> queryQuestionById(int id) {
        Map<String,Object> map=new HashMap<>();
        List<HashMap<String, Object>> list = baseDao.queryObject(" select new Map(" +
                "t1.rid as rid,t1.replyNumber as replyNumber,t1.createTime as createTime,t1.content as content,t2.communityName as communityName," +
                "t3.name as name) from QuestionEntity t1 inner join CommunityEntity t2 on t1.communityId=t2.rid " +
                "inner join UserdetailsEntity t3 on t1.userId=t3.userId " +
                "where t1.rid=?0", id);
        if(list!=null && list.size()>0){
            map.put("obj",list.get(0));
        }
        List<HashMap<String, Object>> list2 = baseDao.queryObject(" select new Map(" +
                "t1.rid as rid,t1.content as content,t1.answerTime as answerTime," +
                "t3.name as name) from QuestiondetailsEntity t1 inner join QuestionEntity t2 on t1.questionId=t2.rid " +
                "inner join UserdetailsEntity t3 on t1.userId=t3.userId " +
                "where t1.questionId=?0", id);
        map.put("list",list2);
        return map;
    }
    //分页查询提问
    @Override
    public PageModel<HashMap<String, Object>> queryQuestionLimit(int limit, int page, String condition) {
        PageModel<HashMap<String, Object>> pm = new PageModel<HashMap<String, Object>>();

        //计算开始位置
        int begin=(page-1)*limit;
        //创建查询对象
        DetachedCriteria dc = DetachedCriteria.forClass(QuestionEntity.class);
        //添加模糊查询条件
        if(condition!=null){
            JSONObject obj = JSON.parseObject(condition);
            if(obj.get("comName")!=null){
                List list = baseDao.queryObject("select t.rid from CommunityEntity t where t.communityName like '%"+obj.get("comName").toString()+"%'");
                dc.add(Restrictions.in("communityId",list));
            }
            if(obj.get("userName")!=null){
                List list = baseDao.queryObject("select t.userId from UserdetailsEntity t where t.name like '%"+obj.get("userName").toString()+"%'");
                dc.add(Restrictions.in("userId",list));
            }
        }

        List<Long> list1 = baseDao.queryObjectCommon(dc.setProjection(Projections.rowCount()));
        dc.setProjection(null);
        dc.setProjection(Projections.property("rid"));
        dc.addOrder(Order.desc("replyNumber"));
        List list = baseDao.queryObjectLimit(dc, begin, limit);
        long i=list1.get(0);
        int count= (int) i;
        List<HashMap<String, Object>> list2=null;
        if(limit!=0){
            list2 = baseDao.queryObject(" select new Map(" +
                    "t1.rid as rid,t1.replyNumber as replyNumber,t1.createTime as createTime,t1.content as content,t2.communityName as communityName," +
                    "t3.name as name) from QuestionEntity t1 inner join CommunityEntity t2 on t1.communityId=t2.rid " +
                    "inner join UserdetailsEntity t3 on t1.userId=t3.userId " +
                    "where t1.rid in (?0)", list);
        }else {
            list2 = baseDao.queryObject(" select new Map(" +
                    "t1.rid as rid,t1.replyNumber as replyNumber,t1.createTime as createTime,t1.content as content,t2.communityName as communityName," +
                    "t3.name as name) from QuestionEntity t1 inner join CommunityEntity t2 on t1.communityId=t2.rid " +
                    "inner join UserdetailsEntity t3 on t1.userId=t3.userId "
            );
        }
        pm.setList(list2);
        pm.setPageNo(page);
        pm.setPageSize(limit);
        if(limit!=0){
            pm.setPagecount(count);
        }
        return pm;
    }
    //根据小区查询提问
    @Override
    public List<HashMap<String, Object>> queryQuestionByCommunity(int id) {
        List<HashMap<String, Object>> list = baseDao.queryObject(" select new Map(" +
                "t1.rid as rid,t1.replyNumber as replyNumber,t1.createTime as createTime,t1.content as content," +
                "t2.communityName as communityName) from QuestionEntity t1 inner join CommunityEntity t2 on t1.communityId=t2.rid  " +
                "where t2.rid=?0", id);
        return list;
    }

    //添加提问
    @Override
    public boolean addQuestion(String data) {
        //创建时间
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        if(data!=null){
            QuestionEntity question = JSON.parseObject(data, QuestionEntity.class);
            question.setCreateTime(timestamp);
            boolean bln = baseDao.addObject(question);
            if(bln)return true;
        }
        return false;
    }

    //删除提问
    @Override
    public boolean delQuestion(String data) {
        if(data!=null){
            //解析json数据
            List<QuestionEntity> list = JSON.parseArray(data, QuestionEntity.class);
            boolean bln = baseDao.delAllObject(list);
            if(bln) return true;
        }
        return false;
    }
    //添加回答
    @Override
    public boolean addQuestionDetails(String data) {
        //创建时间
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        if(data!=null){
            QuestiondetailsEntity question = JSON.parseObject(data, QuestiondetailsEntity.class);
            question.setAnswerTime(timestamp);
            boolean bln = baseDao.addObject(question);
            if(bln){
                //添加回复量
                List<QuestionEntity> list = baseDao.queryObject("from QuestionEntity t1 where t1.rid=?0", question.getQuestionId());
                QuestionEntity quest = list.get(0);
                quest.setReplyNumber(quest.getReplyNumber()+1);
                baseDao.updateObject(quest);
                return true;
            }
        }
        return false;
    }
    //删除回答
    @Override
    public boolean delQuestionDetails(String data) {
        if(data!=null){
            //解析json数据
            QuestiondetailsEntity question = JSON.parseObject(data, QuestiondetailsEntity.class);
            boolean bln = baseDao.delObject(question);
            if(bln) return true;
        }
        return false;
    }
    //根据提问id查询所有回答
    @Override
    public List<QuestiondetailsEntity> SelectQuestionId(int i){
        return baseDao.queryObject("from QuestiondetailsEntity where questionId = ?0 order by rid desc",i);
    }
}
